package com.skp.shaphan.ui.connectioncontrols;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

import com.skp.shaphan.savedConnections.SavedConnection;
import com.skp.shaphan.savedConnections.SavedConnections;

public class SavedConnectionTableModel extends AbstractTableModel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5611190913751811860L;
	private Vector<SavedConnection> conns;
	private HashMap<Integer, JButton> editButtons;

	public SavedConnectionTableModel() {
		SavedConnections sc = SavedConnections.getInstance();
		conns = sc.getConnections();
		editButtons = new HashMap<Integer, JButton>();
	}
	
	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public String getColumnName(int column) {
		if(column == 0) {
			return "Connection Types";
		} else if(column == 1) {
			return "Connection Name";
		} else if(column == 2) {
			return "";
		}
		return "??";
	}

	@Override
	public int getRowCount() {
		return conns.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		if(column == 0) {
			return conns.get(row).getType();
		}
		if(column == 1) {
			return conns.get(row).getName();
		}
		if(column == 2) {
			Integer rowKey = new Integer(row);
			if(editButtons.containsValue(rowKey)) {
				return editButtons.get(rowKey);
			} else {
				JButton editButton = new JButton("Edit");
				editButton.addActionListener(this);
				editButtons.put(rowKey, editButton);
				return editButton;
			}
		}
		return "Error";
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if(columnIndex == 2) {
			return JButton.class;
		} else {
			return String.class;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button pressed!");
	}

}
